﻿using Auto.IRepository.IRepositories;
using Auto.Model.Entities;

using Microsoft.EntityFrameworkCore;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Auto.Repository.Repositories
{
    public class AreaRepository : RepositoryBase<Area>, IAreaRepository
    {
        public AreaRepository(EFDbContext context) : base(context)
        {
        }
        /// <summary>
        /// 获取父级ID集合
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public List<int> GetAllAreaParentID(int ID, List<int> areaIDs)
        {
            var area = dbContext.Areas.Where(a => a.ID == ID && a.IsDeleted == false && a.IsActive == true).FirstOrDefault();
            if (area == null)
            {
                return areaIDs;
            }
            areaIDs.Add(ID);
            var areaParent = dbContext.Areas.Where(a => a.CityCode == area.ParentCode && a.CityCode != a.ParentCode && a.IsDeleted == false && a.IsActive == true).FirstOrDefault();
            if (areaParent == null)
            {
                return areaIDs;
            }
            if (area.LevelType > 1)
            {
                GetAllAreaParentID(areaParent.ID, areaIDs);
            }
            areaIDs.Sort();
            return areaIDs;
        }
    }
}
